package net.tavion.angelboot.fegin.logger;


import net.tavion.angelboot.common.utils.ReflectionUtils;
import org.apache.commons.lang3.StringUtils;

public class ResponseInfo<T> {
    private String action = "response";
    private String appName;
    private String uri;
    private Long duration;
    private Integer status;
    private String loginName;
    private String token;
    private T result;

    public ResponseInfo() {
    }

    public String getUri() {
        return this.uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public static ResponseInfo.Builder builder() {
        return new ResponseInfo.Builder();
    }

    public static ResponseInfo.Builder stringBuilder() {
        return new ResponseInfo.Builder();
    }

    public String getAction() {
        return this.action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public Long getDuration() {
        return this.duration;
    }

    public void setDuration(Long duration) {
        this.duration = duration;
    }

    public Integer getStatus() {
        return this.status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public T getResult() {
        return this.result;
    }

    public String getAppName() {
        return this.appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getLoginName() {
        return this.loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getToken() {
        return this.token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public static class Builder<T> {
        private String action = "response";
        private Long duration;
        private Integer status;
        private String appName;
        private String loginName;
        private String token;
        private T result;
        private String uri;

        public Builder() {
        }

        public ResponseInfo.Builder uri(String uri) {
            this.uri = uri;
            return this;
        }

        public ResponseInfo.Builder action(String action) {
            this.action = action;
            return this;
        }

        public ResponseInfo.Builder duration(Long duration) {
            this.duration = duration;
            return this;
        }

        public ResponseInfo.Builder status(Integer status) {
            this.status = status;
            return this;
        }

        public ResponseInfo.Builder appName(String appName) {
            this.appName = appName;
            return this;
        }

        public ResponseInfo.Builder loginName(String loginName) {
            this.loginName = loginName;
            return this;
        }

        public ResponseInfo.Builder token(String token) {
            this.token = token;
            return this;
        }

        public ResponseInfo.Builder result(T result) {
            this.result = result;
            return this;
        }

        public ResponseInfo<T> build() {
            ResponseInfo<T> responseInfo = new ResponseInfo();
            this.append(responseInfo);
            return responseInfo;
        }

        public <R extends ResponseInfo> ResponseInfo build(Class<R> responseInfoSubClass) {
            ResponseInfo<T> responseInfo = (ResponseInfo) ReflectionUtils.newInstance(responseInfoSubClass);
            this.append(responseInfo);
            return responseInfo;
        }

        private void append(ResponseInfo<T> responseInfo) {
            if (StringUtils.isNotBlank(this.action)) {
                responseInfo.setAction(this.action);
            }

            responseInfo.setDuration(this.duration);
            responseInfo.setStatus(this.status);
            responseInfo.setAppName(this.appName);
            responseInfo.setLoginName(this.loginName);
            responseInfo.setToken(this.token);
            responseInfo.setResult(this.result);
            responseInfo.setUri(this.uri);
        }
    }
}